//
//  GJProgressHUD.m
//  TimeBoat
//
//  Created by yang on 2022/4/21.
//  Copyright © 2022 yang. All rights reserved.
//

#import "GJProgressHUD.h"

@implementation GJProgressHUD

+(instancetype)shareinstance{
    static GJProgressHUD *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[GJProgressHUD alloc] init];
    });
    return instance;
}

+(void)mbLoading{
    [GJProgressHUD mbShow:nil inView:nil mode:GJProgressModeLoading];
}

/// toast  msg 内容
+(void)mbShow:(NSString *__nullable)msg{
    [self show:msg inView:nil mode:GJProgressModeOnlyText customImgView:nil];
}

+(void)mbShow:(NSString *)msg inView:(UIView *)view mode:(GJProgressMode)myMode{
    [self show:msg inView:view mode:myMode customImgView:nil];
}

+(void)show:(NSString *)msg inView:(UIView *)view mode:(GJProgressMode)myMode customImgView:(UIImageView *)customImgView{
    //如果已有弹框，先消失
    if ([GJProgressHUD shareinstance].mb != nil) {
        [[GJProgressHUD shareinstance].mb hideAnimated:YES];
        [GJProgressHUD shareinstance].mb = nil;
    }
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    //4\4s屏幕避免键盘存在时遮挡
    if ([UIScreen mainScreen].bounds.size.height == 480) {
        [view endEditing:YES];
    }
    
    [GJProgressHUD shareinstance].mb = [MBProgressHUD showHUDAddedTo:view animated:YES];

    switch ((NSInteger)myMode) {
        case GJProgressModeOnlyText:
            [GJProgressHUD shareinstance].mb.mode = MBProgressHUDModeText;
            [self mbBaseShowToast:msg];
            break;
        case GJProgressModeLoading:
            [GJProgressHUD shareinstance].mb.mode = MBProgressHUDModeIndeterminate;
            [self mbBaseShowLoadingText:msg];
            break;
        default:
            break;
    }
}

#pragma mark loading
+(void)mbBaseShowLoadingText:(NSString *)text{
    [GJProgressHUD shareinstance].mb.label.text = nil;
    [GJProgressHUD shareinstance].mb.detailsLabel.text = text;
    [GJProgressHUD shareinstance].mb.detailsLabel.font = [UIFont systemFontOfSize:15];
    [[GJProgressHUD shareinstance].mb showAnimated:YES];
}

#pragma mark 文本
+(void)mbBaseShowToast:(NSString *)text{
    [GJProgressHUD shareinstance].mb.detailsLabel.text = text;
    [GJProgressHUD shareinstance].mb.detailsLabel.font = [UIFont systemFontOfSize:15];
    [[GJProgressHUD shareinstance].mb showAnimated:YES];
    [[GJProgressHUD shareinstance].mb hideAnimated:YES afterDelay:1.2];
}

///消失的方法
+(void)mbDismiss{
    [[GJProgressHUD shareinstance].mb hideAnimated:YES];
}

@end
